/* POJ 1006 */
#include <stdio.h>

int main(int argc, char const *argv[])
{
    int cycles[3], today, nextPeak;
    int loop = 1;

    while (scanf("%d%d%d%d", cycles, cycles + 1, cycles + 2, &today) != EOF) {
        if (today == -1)
            break;

        nextPeak = today + 1;

        while ((nextPeak - cycles[0]) % 23)
            nextPeak++;
        while ((nextPeak - cycles[1]) % 28)
            nextPeak += 23;
        while ((nextPeak - cycles[2]) % 33)
            nextPeak += 23 * 28;

        printf("Case %d: the next triple peak occurs in %d days.\n",
               loop++, nextPeak - today);
    }

    return 0;
}
